** Preamble **

clear all
set more off

local user    "alexs"
local project "globo_debate"
local folder  "_replicate_me"

local path_input  "C:/Users/$user/Dropbox/$project/master/output/data"
local path_output "C:/Users/$user/Dropbox/$project/master/output/figures"

* Note: local directory is the output folder 
cd "`path_output'"

					** Loading the input file **

use `path_input'/dataset_final_may20, clear

estimates clear

xtset codigo_ibge turno

local baseline_controls "pop1991 years_sch1991 renda1991 sh_tv1991 sh_agua1991 sh_elect1991 sh_rural"
local CGB_FEs			"pct_sh_tv1991 pct_sh_rural pct_renda1991"


/*generating percentiles*/
foreach var in `baseline_controls' {
xtile pct_`var'=  `var', n(10)
replace pct_`var'=0 if turno==2
}

/*generating pscores*/
encode uf, gen(UF)
psmatch2 Globo T1* if turno==1,  noreplace neighbor(1)
egen ppp=mean(_pscore), by(codigo)
gen pscore=1
replace pscore=ppp/(1-ppp) if Globo==0


** Creating table in Stata Dataframe **

gen variable=""
replace variable="Population [×1,000]"						in 1
replace variable="Years of schooling"						in 2
replace variable="Income per capita [in min. wage]"			in 3
replace variable="Share of households with TV"				in 4
replace variable="Share of households with piped water"		in 5
replace variable="Share of households with electricity"		in 6
replace variable="Share of pop. in rural areas"				in 7



foreach spec of newlist unc cond weig {

	foreach var of newlist ll_st_mean_diff st_mean_diff ul_st_mean_diff t_st_mean_diff {

		gen `var'_`spec'=.
	
	}
}


local vars `baseline_controls'
local num_vars: word count `vars'
di `num_vars'


local i=1
foreach var of varlist `vars' {
 
	numlist "1/`num_vars'"
	local myvar: word `i' of `r(numlist)'
	di `myvar'
	
	egen std_`var' = std(`var')

	** balance check - unconditional **
	reghdfe std_`var' Globo if turno==1, noabsorb vce(cluster codigo_ibge)
	* cluster(codigo)
	matrix  table = r(table)
	replace    st_mean_diff_unc = table[rownumb(table, "b"), colnumb(table,"Globo")] if _n==`myvar'
	replace  t_st_mean_diff_unc = table[rownumb(table, "t"), colnumb(table,"Globo")] if _n==`myvar'
	
	** balance check - group FEs **
	reghdfe std_`var' Globo if turno==1, absorb(`CGB_FEs') vce(cluster codigo_ibge)
	matrix  table = r(table)
	replace    st_mean_diff_cond = table[rownumb(table, "b"), colnumb(table,"Globo")] if _n==`myvar'
	replace  t_st_mean_diff_cond = table[rownumb(table, "t"), colnumb(table,"Globo")] if _n==`myvar'
	   

	** balance check - weights **
	reghdfe std_`var' Globo [pw=pscore] if turno==1, noabsorb vce(cluster codigo_ibge)
	matrix  table = r(table)
	replace    st_mean_diff_weig = table[rownumb(table, "b"), colnumb(table,"Globo")] if _n==`myvar'
	replace  t_st_mean_diff_weig = table[rownumb(table, "t"), colnumb(table,"Globo")] if _n==`myvar'

	drop std_`var'

	local i=`i'+1
	
}


label var pop1991       "Population [×1,000]"
label var years_sch1991 "Years of schooling"
label var renda1991     "Income per capita [in min. wage]"
label var sh_tv1991     "Share of households with TV"
label var sh_agua1991   "Share of households with piped water"
label var sh_elect1991  "Share of households with electricity"
label var sh_rural      "Share of pop. in rural areas"


gen row1 = _n
replace row1=. if _n> 7

gen row2=row1
gen row3=row1
label var row1 "Unconditional" 
label var row2 "Group FEs" 
label var row3 "Reweighted"

order row1 row2 row3 variable-t_st_mean_diff_weig
 
twoway (scatter row1 t_st_mean_diff_unc , mcolor(black) msize(small) msymbol(D)      ///
	    text(7.4 -2.4  "-1.96", size(small)) text(7.4 2.4  "1.96", size(small)))     ///      
	   (scatter row2 t_st_mean_diff_cond, mcolor(navy)  msize(small) msymbol(S))     ///
       (scatter row3 t_st_mean_diff_weig, mcolor(red)   msize(small) msymbol(C)),    ///
	   xline(-1.96, lpattern(dash) lcolor(grey))          		  			         ///
	   xline( 1.96, lpattern(dash) lcolor(grey))          		  			         ///
	   ///
	   ylabel(1 "`: var label pop1991'"          		  			     	 	     /// 
			  2 "`: var label years_sch1991'"          		  			     	     /// 
			  3 "`: var label renda1991'"           		  			     	     ///
			  4 "`: var label sh_tv1991'"           		  			     	     ///
			  5 "`: var label sh_agua1991'"           		  			     	     ///
			  6 "`: var label sh_elect1991'"           		  			     	     ///
			  7 "`: var label sh_rural'", angle(0) noticks nogrid labsize(small))    ///
	   xtitle("T-statistic", size(8pt))                				  			     ///
	   graphregion(color(none)) 								  				     ///
       bgcolor(white) legend(cols(1)) saving("Figure_Balance_Check_Graph_A.png", replace)
	   
	   graph export Figure_Balance_Check_Graph_A.png, replace

	   
   
	   
twoway (scatter row1 st_mean_diff_unc , mcolor(black) msize(small) msymbol(D)     ///
	    text(7.4 -.06  "-.05", size(small)) text(7.4 .06  ".05", size(small)))    ///
	   (scatter row2 st_mean_diff_cond, mcolor(navy)  msize(small) msymbol(S)) 	  ///
	   (scatter row3 st_mean_diff_weig, mcolor(red)   msize(small) msymbol(C)),   ///
  	   xline(-0.05, lpattern(dash) lcolor(grey))          		  			      ///
	   xline( 0.05, lpattern(dash) lcolor(grey))          		  			      ///
	   ///
	   ylabel(1 "`: var label pop1991'"          		  			     	 	     /// 
			  2 "`: var label years_sch1991'"          		  			     	     /// 
			  3 "`: var label renda1991'"           		  			     	     ///
			  4 "`: var label sh_tv1991'"           		  			     	     ///
			  5 "`: var label sh_agua1991'"           		  			     	     ///
			  6 "`: var label sh_elect1991'"           		  			     	     ///
			  7 "`: var label sh_rural'", angle(0) noticks nogrid labsize(small))    ///
	   xtitle("Standardized mean difference", size(8pt))				  		     ///
	   graphregion(color(none)) 									  			     ///
	   bgcolor(white) legend(cols(1)) saving("Figure_Balance_Check_Graph_B_with_legends.png", replace)

	   graph export Figure_Balance_Check_Graph_B_with_legends.png, replace

